package pzl.com.drawrelated.views;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.view.View;

import pzl.com.drawrelated.R;

/**
 * @author zl.peng
 * @version [1.0, 2016-11-21]
 */
public class V_BitmapShader extends View {
    int w;
    int h;
    Paint paint;
    Bitmap biger;
    Bitmap smaller;
    Bitmap repeat;
    Bitmap mirror;

    BitmapShader smallerBitmapShaderCLAMP;
    BitmapShader bigerBitmapShaderCLAMP;
    BitmapShader bitmapShaderREPEAT;
    BitmapShader bitmapShaderMIRROR;

    public V_BitmapShader(Context context) {
        super(context);
    }

    public V_BitmapShader(Context context, AttributeSet attrs) {
        super(context, attrs);
        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setStrokeWidth(1);
        biger = BitmapFactory.decodeResource(context.getResources(), R.drawable.test1);
        smaller = BitmapFactory.decodeResource(context.getResources(), R.drawable.menu_circle);
        repeat = BitmapFactory.decodeResource(context.getResources(), R.drawable.menu_circle);
        mirror = BitmapFactory.decodeResource(context.getResources(), R.drawable.love);
        /**
         *CLAMP：当bitmap比要绘制的图形小时拉伸位图的最后一个像素；
         * 当bitmap比要绘制的图形大时，根据绘制图形剪裁bitmap
         */
        smallerBitmapShaderCLAMP = new BitmapShader(smaller, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
        bigerBitmapShaderCLAMP = new BitmapShader(biger, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
        bitmapShaderREPEAT = new BitmapShader(repeat, Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);
        bitmapShaderMIRROR = new BitmapShader(mirror, Shader.TileMode.MIRROR, Shader.TileMode.MIRROR);
    }

    public V_BitmapShader(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        w = getWidth();
        h = getHeight();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        paint.setShader(smallerBitmapShaderCLAMP);
        Rect rectBigger = new Rect();
        rectBigger.top = 20;
        rectBigger.left = 20;
        rectBigger.right = w - 20;
        rectBigger.bottom = rectBigger.top + 400;
        canvas.drawRect(rectBigger, paint);

        Rect rectSmaller = new Rect();
        rectSmaller.top = rectBigger.bottom + 20;
        rectSmaller.left = 200;
        rectSmaller.right = w - 200;
        rectSmaller.bottom = rectSmaller.top + 100;
        paint.setShader(bigerBitmapShaderCLAMP);
        canvas.drawRect(rectSmaller, paint);

        Rect rectRepeat = new Rect();
        rectRepeat.top = rectSmaller.bottom + 20;
        rectRepeat.left = 0;
        rectRepeat.right = w - 0;
        rectRepeat.bottom = rectSmaller.top + 400;
        paint.setShader(bitmapShaderREPEAT);
        canvas.drawRect(rectRepeat, paint);

        Rect rectMirror = new Rect();
        rectMirror.top = rectRepeat.bottom + 20;
        rectMirror.left = 0;
        rectMirror.right = w - 0;
        rectMirror.bottom = rectMirror.top + 220;
        paint.setShader(bitmapShaderMIRROR);
        canvas.drawRect(rectMirror, paint);

    }
}
